#!/usr/bin/evn python
# -*- coding: utf-8 -*-
from pydantic import BaseModel, ValidationError
from pydantic import model_validator


class User(BaseModel):
    username: str
    password_old: str
    password_new: str

    @model_validator(mode='after')
    def check_passwords(self):
        password_old, password_new = self.password_old, self.password_new
        # 新旧号码的确认匹配处理
        if password_old and password_new and password_old != password_new:
            raise ValueError('passwords do not match')
        return self


if __name__ == '__main__':
    try:
        user = User(username='xiaozhong', password_old='123456', password_new='123456_')
    except ValidationError as e:
        print(e.errors())
    else:
        print(user.username, user.password_old)
